package com.homework.note.Controller;

import com.homework.note.Service.NotebookService;
import com.homework.note.Util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.InvalidParameterException;

/**
 * @author htyu
 * @date 2019/3/9
 */
@RestController
@RequestMapping(value = "notebook")
public class NotebookController {

    @Autowired
    NotebookService notebookService;

    /**
     * 新建笔记本
     * @param bookName
     * @return
     * @throws Throwable
     */
    @PostMapping("create")
    public Message createBook(@RequestParam(required = false) String fatherID,@RequestParam String bookName) throws Throwable {
        try{
            return Message.success(notebookService.create(fatherID,bookName));
        }catch (RuntimeException e){
            return Message.error(-1,"上一级笔记本不存在");
        }
    }

    /**
     * 获取笔记本列表
     * @return
     */
    @GetMapping("list")
    public Message bookList(@RequestParam String fatherID) throws Throwable {
        return Message.success(notebookService.getBookList(fatherID));
    }


    /**
     * 删除笔记本
     * @param bookID
     * @return
     */
    @DeleteMapping("delete")
    public Message deleteBook(@RequestParam String bookID){
        try {
            return Message.success(notebookService.deleteBook(bookID));
        }catch (RuntimeException e){
            return Message.error(-1,"上一级笔记本不存在");
        }
    }

    /**
     * 重命名笔记本
     * @param bookID
     * @param name
     * @return
     */

    @PutMapping("rename")
    public Message bookRename(@RequestParam String bookID,@RequestParam String name){
        try {
            return Message.success(notebookService.rename(bookID, name));
        }catch(RuntimeException e){
            return Message.error(-1,"该笔记本不存在");
        }
    }

    /**
     * 移动笔记本
     * @param moveID
     * @param fatherID
     * @return
     */

    @PutMapping("move")
    public Message move(@RequestParam String moveID,@RequestParam String fatherID){
        try {
            return Message.success(notebookService.move(moveID, fatherID));
        }catch (InvalidParameterException e){
            return Message.error(-1,"待移动笔记本不存在");
        }catch(RuntimeException e){
            return Message.error(-1,"目标笔记本不存在");
        }
    }

    /**
     * 复制笔记本
     * @param copyID
     * @param fatherID
     * @return
     */

    @PostMapping("copy")
    public Message copy(@RequestParam String copyID,@RequestParam String fatherID){
        try {
            return Message.success(notebookService.copy(copyID, fatherID));
        }catch (InvalidParameterException e){
            return Message.error(-1,"待移动笔记本不存在");
        }catch(RuntimeException e){
            return Message.error(-1,"目标笔记本不存在");
        }
    }


}
